﻿$(document).ready(function () {
    $("#btn-closePopup").click(function () {
        parent.$.colorbox.close(); //Đóng popup
    });
    //
    $('#cbCheckAll').click(function () {
        var c = this.checked;
        $(':checkbox').prop('checked', c);
        xCalMoney();
    });
    //
    $(".cb-hours").click(function () {
        xCalMoney();
    });
    //
    $('#startDate').change(function () {
        xCalMoney();
    });
    //
    $('#endDate').change(function () {
        xCalMoney();
    });
    //
    $("#btn-submit").click(function () {
        xSubmit();
    });
});
/*======================================UP TIN TU DONG=================================*/
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var dtTimeBatDau = null;
var dtTimeKetThuc = null;
var intSoNgayUpTin = 0;
var intSoLanUpTin = 0;

function xCalMoney() {
    dtTimeBatDau = null;
    dtTimeKetThuc = null;
    intSoNgayUpTin = 0;
    intSoLanUpTin = 0;

    var startDate = $("#startDate").val().split("/");
    var endDate = $("#endDate").val().split("/");

    /*ngay-thang-nam bat dau*/
    var intNgaybd = startDate[0];
    var intThangbd = startDate[1];
    var intNambd = startDate[2];
   
    dtTimeBatDau = Date(intNambd, intThangbd, intNgaybd);
    dtTimeBatDau = new Date(dtTimeBatDau);

    /*ngay-thang-nam ket thuc*/
    var intNgaykt = endDate[0];
    var intThangkt = endDate[1];
    var intNamkt = endDate[2];
    dtTimeKetThuc = Date(intNamkt, intThangkt, intNgaykt);
    dtTimeKetThuc = new Date(dtTimeKetThuc);
    //Tinh so ngay up tin
    intSoNgayUpTin = Math.round(Math.abs((dtTimeBatDau.getTime() - dtTimeKetThuc.getTime()) / (oneDay))) + 1;

    //Tinh so lan up tin/ ngay
    $(".spaceTime .cb-hours:checked").each(function () {
        intSoLanUpTin++;
    });
    //
    var iSoTienPhaiTT1Ngay = intSoLanUpTin * parseInt($(".gia-up-tin").text());
    var iTongSoTienPhaiTT = iSoTienPhaiTT1Ngay * intSoNgayUpTin;
    //
    $(".gold-pay-up-tu-dong").text(iTongSoTienPhaiTT);
}
//Submit
function xSubmit() {
    
    var _dtTimeBatDau = dtTimeBatDau;
    var lstKhungGio = new Array();
    var iCount = 0;
    var strMaTinDang = $('.lblPT_ID').text();
    var lstData = "";
    var strUS_ID = $('.hdUS_ID').val();
    $(".spaceTime .cb-hours:checked").each(function () {
        lstKhungGio[iCount] = $(this).val();
        iCount++;
    });
    //Ghep chuoi
    for (var i = 0; i < intSoNgayUpTin; i++) {
        for (var j = 0; j < iCount; j++) {
            var strUpTime = _dtTimeBatDau.getFullYear() + "/" + (_dtTimeBatDau.getMonth()+1)+ "/" + _dtTimeBatDau.getDate() + " " + lstKhungGio[j];
            lstData += strUS_ID + "," + strMaTinDang + "," + strUpTime + ";";
        }
        if (intSoNgayUpTin > 1) {
            _dtTimeBatDau = addDays(_dtTimeBatDau, 1);
        }
    }
    var iSoTienPhaiThanhToan = $('.gold-pay-up-tu-dong').text();
    //alert(lstData + "\n" + iSoTienPhaiThanhToan + "\n" + strUS_ID);
    $.ajax({
        type: "POST",
        url: "/Default.aspx/fncAuto_Post",
        data: '{strListData:"' + lstData + '"'
                + ',iGold:' + iSoTienPhaiThanhToan
                + ',strUSID:"' + strUS_ID + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var strResult = response.d;
            if (strResult == "") {
                $('#notifition').slideUp("fast");
                alert("Lên lịch up tự động thành công.!");
                parent.$.colorbox.close();
            } else {
                alert(strResult);
            }
        },
        failure: function (response) {
            //alert("failure: " + response.d);
        },
        error: function (response) {
            //alert("error: " + response.d);
        }
    });
}

//Add day to date
function addDays(myDate, days) {
    return new Date(myDate.getTime() + days * 24 * 60 * 60 * 1000);
}
/*============================================================================================*/